<template>
  <el-dialog title="选择分组" :visible.sync="visible" width="800px" top="5vh" append-to-body>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="分组名称" prop="deptName">
        <el-input
          v-model="queryParams.deptName"
          placeholder="请输入分组名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="deptList"
      row-key="deptId"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="deptName" label="分组名称"></el-table-column>
      <el-table-column prop="orderNum" label="排序" width="50"></el-table-column>
    </el-table>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleSelectDept">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listDept } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  dicts: ['sys_normal_disable','sys_dept_type'],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      deptList: [],
      // 部门树选项
      deptOptions: [],
      rtnDepts:[],
      // 弹出层标题
      title: "",
      visible:false,
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      ids:null,
      // 查询参数
      queryParams: {
        excludeTypes: '3',
        deptName: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 根节点ID
      firstId: undefined,
      // 表单校验
    };
  },
  created() {
    
  },
  watch: {
    form: {
      deep: true,
      handler(v) {
        this.form.showType = v.parentId == this.firstId
      }
    },
  },
  methods: {
    // 显示弹框
    show(idx) {
      this.rtnDepts = []
      this.getList(true);
      this.visible = true;
      this.idx = idx;
    },
    handleSelectPolice(){},
    /** 查询部门列表 */
    getList(init) {
      this.loading = true;
      listDept(this.queryParams).then(response => {
        this.deptList = this.handleTree(response.data, "deptId");
        this.loading = false;
        if (init) {
          this.firstId = this.deptList && this.deptList.filter(o => String(o.parentId) == '0').map(o => o.deptId).pop();
        }
      });
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.rtnDepts = selection;
    },
    handleSelectDept(){
      if(!this.rtnDepts)
      {
        this.$modal.msgError("最少选择一行数据");
        return;
      }
      this.$emit("ok",this.rtnDepts) 
      this.visible = false
    }
  }
};
</script>
